package com.example.teachingevaluationsystem.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.teachingevaluationsystem.entity.EvaluationPlan;
import com.example.teachingevaluationsystem.entity.PlanTopic;
import com.example.teachingevaluationsystem.entity.User;
import com.example.teachingevaluationsystem.service.EvaluationPlanService;
import com.example.teachingevaluationsystem.service.IUserService;
import com.example.teachingevaluationsystem.service.PlanTopicService;
import com.example.teachingevaluationsystem.vo.EvaluationPlanVo;
import com.example.teachingevaluationsystem.vo.PlanSubmitVo;
import lombok.AllArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import java.security.Principal;
import java.util.Arrays;
import java.util.List;

/**
 * 评价计划 前端控制器
 *
 * @author lp
 * @since 2022-04-04
 */
@RestController
@RequestMapping("/evaluate/evaluationPlan")
@AllArgsConstructor
public class EvaluationPlanController {

  private EvaluationPlanService evaluationPlanService;
  private PlanTopicService planTopicService;
  private IUserService userService;

  /** 分页查询 评价计划 */
  @GetMapping("page")
  public Page<EvaluationPlan> evaluationPlanPage(
      Page<EvaluationPlan> evaluationPlanPage,
      @RequestParam(value = "status", required = false) Integer status) {
    final LambdaQueryWrapper<EvaluationPlan> queryWrapper = new LambdaQueryWrapper<>();
    if (status != null) {
      queryWrapper.eq(EvaluationPlan::getStatus, status);
    }
    return evaluationPlanService.page(evaluationPlanPage, queryWrapper);
  }

  /** 保存或者 更新 评价计划 */
  @PostMapping
  public boolean save(@RequestBody EvaluationPlanVo evaluationPlan) {
    return evaluationPlanService.save(evaluationPlan);
  }

  /** 通过id删除题目 */
  @DeleteMapping
  public boolean deleteByIds(@RequestBody Long[] ids) {
    return evaluationPlanService.removeBatchByIds(Arrays.asList(ids));
  }

  @PutMapping("{planId}/status")
  public boolean updateStatus(Integer status, @PathVariable String planId) {
    return evaluationPlanService.update(
        new LambdaUpdateWrapper<EvaluationPlan>()
            .eq(EvaluationPlan::getId, planId)
            .set(EvaluationPlan::getStatus, status));
  }

  /** 获取详情 */
  @GetMapping("{planId}")
  public EvaluationPlanVo getPlanInfo(@PathVariable("planId") Long planId) {
    final EvaluationPlan evaluationPlan = evaluationPlanService.getById(planId);
    final EvaluationPlanVo planVo = new EvaluationPlanVo();
    BeanUtils.copyProperties(evaluationPlan, planVo);
    final Long[] topicIds =
        planTopicService
            .list(
                new LambdaUpdateWrapper<PlanTopic>()
                    .eq(PlanTopic::getPlanId, evaluationPlan.getId()))
            .stream()
            .map(PlanTopic::getTopicId)
            .toArray(Long[]::new);
    Arrays.stream(topicIds).findAny().ifPresent(planVo::setTopicId);
    return planVo;
  }

  @PostMapping("/student/{planId}/{courseId}")
  public void submit(
      Principal principal,
      @RequestBody List<PlanSubmitVo> submitVoList,
      @PathVariable Long courseId,
      @PathVariable Long planId) {
    User user = userService.loadUserByUserName(principal.getName());
    evaluationPlanService.save(user.getId(), submitVoList, courseId, planId, false);
  }

  @PostMapping("/teacher/{planId}/{courseId}")
  public void teacherSubmit(
      Principal principal,
      @RequestBody List<PlanSubmitVo> submitVoList,
      @PathVariable Long courseId,
      @PathVariable Long planId) {
    final User user = userService.loadUserByUserName(principal.getName());
    evaluationPlanService.save(user.getId(), submitVoList, courseId, planId, true);
  }

  /** 查询所有的计划 */
  @GetMapping
  public List<EvaluationPlan> getEvaluationPlanList() {
    return evaluationPlanService.list();
  }
}
